package com.lalosoft.misgastos.config;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;
import android.content.SharedPreferences;

import com.lalosoft.misgastos.dao.AccountDao;
import com.lalosoft.misgastos.dao.ItemDao;
import com.lalosoft.misgastos.domain.Account;
import com.lalosoft.misgastos.domain.Item;


public class GastosManager {

//	private DatabaseHelper helper;
	private SharedPreferences settings;
	private boolean firstTime;
	private boolean avisosMensuales;
	private long lastRecalculate;
	private String username;
	private String password;
	private Account currentAccount;
	private boolean started;
	
	private AccountDao accountDao;
	private ItemDao itemDao;
	
	private static GastosManager instance = null;
	
	public static GastosManager getInstance(Context ctx){
		if(instance == null){
			instance = new GastosManager(ctx);
		}
		return instance;
	}
	
	private GastosManager(Context context){
//		OpenHelperManager.setOpenHelperClass(DatabaseHelper.class);
//		helper = (DatabaseHelper)OpenHelperManager.getHelper(context);
		accountDao = new AccountDao(context);
		itemDao = new ItemDao(context);
		loadSettings(context);
	}

//	public int saveAccount(Account account) throws SQLException{
//		return helper.getAccountDao().create(account);
//	}
	
//	public Account getAccount(String username, String password) throws SQLException{
////		List<Account> list = null;
////		Map<String, Object> fieldValues = new HashMap<String, Object>();
////		fieldValues.put("username", username);
////		fieldValues.put("password", password);
////		list = helper.getAccountDao().queryForFieldValues(fieldValues);
////		if(list.size() > 0){
////			return list.get(0);
////		} else {
////			return null;
////		}
//	}

	public List<Item> getAllItems() throws SQLException{
//		List<Item> list = new ArrayList<Item>();
//		list = helper.getItemDao().queryForAll();
//		return list;
		return itemDao.findAll();
	}
	
	public boolean isFirstTime(){
		return firstTime;
	}
	
//	public String getAesKey(){
//		return aesKey;
//	}
//	
//	public void setAesKey(String text) throws Exception{
//		this.aesKey = text;
//		SharedPreferences.Editor editor = settings.edit();
//		editor.putString(Constants.AES_KEY, aesKey);
//		editor.commit();
//	}
	
	public void logged(){
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constants.USERNAME, username);
		editor.commit();
	}
	
	public boolean isLogged(){
		return !username.equals("") && !password.equals("");
	}
	
	public void setCurrentAccount() throws SQLException{
//		Account account = getAccount(username, password);
		Account account = accountDao.findByUserAndPass(username, password);
		currentAccount = account;
	}
	
	public Account getCurrentAccount(){
		return currentAccount;
	}
	
	public void setUser(Account account) throws SQLException{
		saveLocalUser(account);
//		saveAccount(account);
		accountDao.insert(account);
		currentAccount = account;
	}
	
	public void setFirstTime(boolean value){
		this.firstTime = value;
		saveSettings();
	}
	
	public boolean avisosMensualesActivado(){
		return avisosMensuales;
	}
	
	public void avisosMensuales(boolean valor){
		this.avisosMensuales = valor;
		saveSettings();
	}
	
	public long lastRecalculate(){
		return lastRecalculate;
	}
	
	public void setLastRecalculate(long value){
		this.lastRecalculate = value;
	}
	
	public void updateAccount(Account account) throws SQLException{
		saveLocalUser(account);
		currentAccount = account;
		accountDao.update(account);
//		helper.getAccountDao().update(account);
	}
	
	public void deleteItem(Item item) throws SQLException{
//		int id = helper.getItemDao().delete(item);
//		if(getAllItems().size() == 0){
//			setFirstTime(true);
//		}
//		return id;
		itemDao.delete(item);
	}
	
	public void deleteAllItems() throws SQLException{
		List<Item> list = getAllItems();
		for(Item temp : list){
			deleteItem(temp);
		}
	}
	
	public void updateItem(Item item) throws SQLException{
//		return helper.getItemDao().update(item);
		itemDao.update(item);
	}
	
	public void saveItem(Item item) throws SQLException{
//		return helper.getItemDao().create(item);
		itemDao.insert(item);
	}
	
	public static String saveToJSon(Object request) throws JsonGenerationException, JsonMappingException, IOException {
		   ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
		   return mapper.writeValueAsString(request);
	}
	
	public void started(){
		this.started = true;
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(Constants.STARTED, started);
		editor.commit();
	}
	
	public boolean isStarted(){
		return started;
	}
	
	private void saveLocalUser(Account account){
		this.username = account.getUsername();
		this.password = account.getPassword();
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constants.USERNAME, username);
		editor.putString(Constants.PASSWORD, password);
		editor.commit();
	}
	
	private void loadSettings(Context ctx) {
		settings = ctx.getSharedPreferences(Constants.PREFS_NAME, 0);
		firstTime = settings.getBoolean(Constants.FIRST_TIME, true);
		avisosMensuales = settings.getBoolean(Constants.REMEMBER, true);
		lastRecalculate = settings.getLong(Constants.LAST_RECAL, 0);
		username = settings.getString(Constants.USERNAME, "");
		password = settings.getString(Constants.PASSWORD, "");
		started = settings.getBoolean(Constants.STARTED, false);
	}
	
	public void saveSettings(){
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(Constants.FIRST_TIME, firstTime);
		editor.putBoolean(Constants.REMEMBER, avisosMensuales);
		editor.putLong(Constants.LAST_RECAL, lastRecalculate);
		editor.commit();
	}

}
